using System;

namespace Tool.PEMath
{
    public struct PEArgs
    {
        public int value;
        public uint multipler;

        public PEArgs(int value, uint multipler)
        {
            this.value = value;
            this.multipler = multipler;
        }

        public static readonly PEArgs Zero = new PEArgs(0, 10000);
        public static readonly PEArgs HALFPI = new PEArgs(15708, 10000);
        public static readonly PEArgs PI = new PEArgs(31416, 10000);
        public static readonly PEArgs TWOPI = new PEArgs(62832, 10000);

        public static bool operator >(PEArgs a, PEArgs b)
        {
            if (a.multipler == b.multipler)
                return a.value > b.value;
            else
                throw new System.Exception("multipler is unequal.");
        }

        public static bool operator <(PEArgs a, PEArgs b)
        {
            if (a.multipler == b.multipler)
                return a.value < b.value;
            else
                throw new System.Exception("multipler is unequal.");
        }

        public static bool operator >=(PEArgs a, PEArgs b)
        {
            if (a.multipler == b.multipler)
                return a.value >= b.value;
            else
                throw new System.Exception("multipler is unequal.");
        }

        public static bool operator <=(PEArgs a, PEArgs b)
        {
            if (a.multipler == b.multipler)
                return a.value <= b.value;
            else
                throw new System.Exception("multipler is unequal.");
        }

        public static bool operator ==(PEArgs a, PEArgs b)
        {
            if (a.multipler == b.multipler)
                return a.value == b.value;
            else
                throw new System.Exception("multipler is unequal.");
        }

        public static bool operator !=(PEArgs a, PEArgs b)
        {
            if (a.multipler == b.multipler)
                return a.value != b.value;
            else
                throw new System.Exception("multipler is unequal.");
        }

        
        /**
         * 转化为视图角度，不可再用于逻辑运算,只能用于表现层
         */
        public int ConvertViewAngle() => (int) Math.Round(ConvertToFloat() / Math.PI * 180);
        
        /**
         * 转化为视图弧度，不可再用于逻辑运算,只能用于表现层
         */
        public float ConvertToFloat() => value * 1.0f / multipler;

        public static PEInt ConvertAngleToValue(PEInt angle)
        {
            return angle * PEInt.pi / 180 * (int)AcosTable.Multipler;
        }

        public override bool Equals(object obj)
        {
            return obj is PEArgs args &&
                   value == args.value &&
                   multipler == args.multipler;
        }

        public override int GetHashCode()
        {
            return value.GetHashCode();
        }

        public override string ToString()
        {
            return $"value:{value} multipler:{multipler}";
        }
    }
}